﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
using System.Data.Common;
namespace ESHOP.DAO
{
    public class HopthuDAO:BaseDAO
    {
        public HOPTHU Get(string ma)
        {
            return _db.HOPTHUs.Where(p => p.ID==ma).SingleOrDefault();
        }
        public List<HOPTHU> GetListbyCompaniesSend(String madngui)
        {
            return _db.HOPTHUs.Where(p => p.SENDER.Equals(madngui) && p.ISSHOW.Equals(true)).ToList();
        }
        public List<HOPTHU> GetListbyCompaniesReceiver_new(String mact)
        {
            return _db.HOPTHUs.Where(p => p.RECEIVER.Equals(mact) && p.ISSHOW.Equals(true) && p.IsRead.Equals(false)).OrderByDescending(p => p.NGAYGUI).ToList();
        }
        public List<HOPTHU> GetListbyCompaniesReceiver_old(String mact)
        {
            return _db.HOPTHUs.Where(p => p.RECEIVER.Equals(mact) && p.ISSHOW.Equals(true) && p.IsRead.Equals(true)).OrderByDescending(p => p.NGAYGUI).ToList();
        }
        public List<HOPTHU> Getlistoutboxbycompanies(String mact)
        {
            return _db.HOPTHUs.Where(p => p.SENDER.Equals(mact) && p.ISSHOW.Equals(true)).OrderByDescending(p => p.NGAYGUI).ToList();
        }
        public List<HOPTHU> Getlistdeletecompanies(String mact)
        {
            return _db.HOPTHUs.Where(p => p.ISSHOW.Equals(false) && (p.SENDER.Equals(mact) || p.RECEIVER.Equals(mact))).OrderByDescending(p => p.NGAYGUI).ToList();
        }
        public int DeleteMail(string Id)
        {
            try
            {
                var objdb = Get(Id);
                if (objdb != null)
                {
                    objdb.ISSHOW = false;
                    _db.SubmitChanges();
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }

        public int Updateread(HOPTHU objui)
        {

            try
            {
                var objdb = Get(objui.ID);
                if (objdb != null)
                {
                    objdb.IsRead = true;
                    _db.SubmitChanges();
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }

        public string NewId()
        {
            string sReturn = "";

            string sToday = "";
            sToday = DateTime.Now.Year.ToString();
            sToday = DateTime.Now.Month < 10
                         ? sToday + "0" + DateTime.Now.Month.ToString()
                         : sToday + DateTime.Now.Month.ToString();

            sToday = DateTime.Now.Day < 10
                         ? sToday + "0" + DateTime.Now.Day.ToString()
                         : sToday + DateTime.Now.Day.ToString();

            sReturn = (from p in _db.HOPTHUs.Where(p => p.ID.Substring(0, 8).Contains(sToday))
                       select p.ID).Max();

            if (!string.IsNullOrEmpty(sReturn))
            {
                int thutu = int.Parse(sReturn.Substring(8, 3));
                thutu = thutu + 1;
                if (thutu < 10)
                {
                    sReturn = sToday + "00" + thutu.ToString();
                }
                else if (thutu < 100)
                {
                    sReturn = sToday + "0" + thutu.ToString();
                }
                else if (thutu < 1000)
                {
                    sReturn = sToday + thutu.ToString();
                }
            }
            else
            {
                sReturn = sToday + "001";
            }

            return sReturn;
        }
        public int Insert(HOPTHU objui)
        {
            try
            {
                _db.HOPTHUs.InsertOnSubmit(objui);

                _db.SubmitChanges();
                return 1;
            }
            catch
            {
                return 0;
            }
        }


    }
}
